package com.small.base.util;

import com.small.base.constants.NumCommon;
import com.small.base.constants.StrCommon;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

/**
 * AesUtil
 *
 * @author miajio
 */
public class AesUtil {

    /**
     * 解密数据
     * @param var 待解密数据 -> 二进制
     * @param key 密钥 -> 二进制
     * @return 解密后数据 -> 二进制
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    public static byte[] decrypt(byte [] var, byte [] key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Key inKey = toKey(key);
        Cipher cipher = Cipher.getInstance(StrCommon.CIPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, inKey);
        return cipher.doFinal(var);
    }

    /**
     * 加密数据
     * @param var 待加密数据 -> 二进制
     * @param key 密钥 -> 二进制
     * @return 加密后数据 -> 二进制
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    public static byte[] encrypt(byte [] var, byte [] key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Key inKey = toKey(key);
        Cipher cipher = Cipher.getInstance(StrCommon.CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, inKey);
        return cipher.doFinal(var);
    }

    /**
     * private 创建密钥
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static byte[] createKey() throws NoSuchAlgorithmException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(StrCommon.AES);
        keyGenerator.init(NumCommon.INT_256);
        SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }

    /**
     * 密钥转换
     * @param key 二进制密钥
     * @return
     */
    private static Key toKey(byte [] key) {
        return new SecretKeySpec(key, StrCommon.AES);
    }

//    public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {
//        byte [] key = "1234567891234567".getBytes();
//        System.out.println(StringUtil.encodeBase64(key));
//        byte [] content = encrypt("测试一波不会死吧".getBytes(), key);
//        System.out.println("content:" + new String(content));
//
//
//        String encode = StringUtil.encodeBase64(content);
//        System.out.println("encode:" + encode);
//        byte[] decode = StringUtil.decodeBase64(encode);
//        System.out.println("decode:" + new String(decode));
//        System.out.println(new String(decrypt(decode, key)));
//
//    }
}
